//
//  HotkeyWordsView.m
//  B5M
//
//  Created by lienbao on 25/07/2014.
//  Copyright (c) 2014 b5m. All rights reserved.
//

#import "HotkeyWordsView.h"
#import "UIView+Borders.h"
#import "UIComponent.h"

@interface HotkeyWordsView ()

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation HotkeyWordsView

- (id)initWithFrame:(CGRect)frame hotkeyWords:(NSArray *)hotKeyWords
{
    return [self initWithFrame:frame hotkeyWords:hotKeyWords rowLimit:3];
}

- (id)initWithFrame:(CGRect)frame hotkeyWords:(NSArray *)hotKeyWords rowLimit:(NSInteger)limit
{
    return [self initWithFrame:frame hotkeyWords:hotKeyWords rowLimit:limit contentInset:UIEdgeInsetsMake(0, 10, 0, 10)];
}

- (id)initWithFrame:(CGRect)frame hotkeyWords:(NSArray *)hotKeyWords rowLimit:(NSInteger)limit contentInset:(UIEdgeInsets)inset autoSize:(BOOL)atuoSize
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 36)];
        title.text = @"     热门区域";
        title.font = [UIFont systemFontOfSize:16.0f];
        title.textColor = [UIColor colorWithHex:0x586576 alpha:1.0f];
        title.backgroundColor = [UIColor whiteColor];
        [self addSubview:title];
        _titleLabel = title;
        
        //[_titleLabel addTopBorderWithHeight:0.5f andColor:[UIColor colorWithRGBHexString:@"0xcbcbcb"]];
        [_titleLabel addBottomBorderWithHeight:0.5f andColor:[UIColor colorWithHex:0xcbcbcb alpha:1.0f]];
        
        NSArray *colors = @[@"c6d2bb", @"ecd2a8", @"f2c4c9", @"afdae3", @"afbfeb", @"eac7ae"];
        
        CGFloat verGap = 10;
        CGFloat horGap = 10;
        NSInteger rows = 0;
        NSInteger height = 36;
        CGFloat x = inset.left;
        CGFloat y = verGap + 36;
        
        if (atuoSize) {
            NSInteger index = 0;
            for ( NSString *keyword in hotKeyWords ) {
                
                if (rows >= limit) {
                    break;
                }
                
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.layer.cornerRadius = 3.0f;
                btn.layer.borderWidth = 1.0f;
                btn.layer.borderColor = [UIColor whiteColor].CGColor;
                btn.backgroundColor = [UIColor colorWithRGBHexString:[colors objectAtIndex:index%colors.count]];
                [btn setTitle:keyword forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                CGSize size = [keyword rtSizeWithFont:btn.titleLabel.font];
                if (x + size.width + horGap > (frame.size.width - inset.left - inset.right)) {
                    y += (verGap + height);
                    x = inset.left;
                    rows++;
                }
                btn.frame = CGRectMake(x, y, size.width, height);
                x += (size.width + horGap);
                btn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
                index++;
            }

        }else {
            NSInteger columon = 3;
            CGFloat width  = (self.width - inset.left - inset.right - (columon - 1)* horGap)/columon;
            for ( NSInteger index = 0; index < hotKeyWords.count; index++ ) {
                
                if (rows >= limit) {
                    break;
                }
                
                if (index && 0 == (index%columon) ) {
                    y += (verGap + height);
                    x = inset.left;
                    rows++;
                }
                
                UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
                btn.layer.cornerRadius = 3.0f;
                btn.layer.borderWidth = 1.0f;
                btn.layer.borderColor = [UIColor whiteColor].CGColor;
                btn.backgroundColor = [UIColor colorWithRGBHexString:[colors objectAtIndex:index%colors.count]];
                [btn setTitle:[hotKeyWords objectAtIndex:index] forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
                [self addSubview:btn];
                
                btn.frame = CGRectMake(x, y, width, height);
                x += (width + horGap);
                btn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
            }
        }
        frame.size.height = y + height + verGap;
        self.frame = frame;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame hotkeyWords:(NSArray *)hotKeyWords rowLimit:(NSInteger)limit contentInset:(UIEdgeInsets)inset
{
   return [self initWithFrame:frame hotkeyWords:hotKeyWords rowLimit:limit contentInset:UIEdgeInsetsMake(0, 10, 0, 10) autoSize:YES];
}

- (void)btnClicked:(UIButton *)sender
{
    if (self.selectKeywordsBlock) {
        self.selectKeywordsBlock(sender.titleLabel.text);
    }
}

@end
